package com.treasure.mall.biz.entity.user;

import com.treasure.mall.biz.base.UserDetails;
import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.biz.entity.embeddable.BusinessEntity;
import com.treasure.mall.biz.entity.embeddable.MerchantData;
import com.treasure.mall.zbq.common.ienum.auth.RoleEnum;
import com.treasure.mall.zbq.common.ienum.live.LiveEnums;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/11
 */
@Entity
@Table(name = "zb_merchant")
@Setter
@Getter
public class MerchantPO extends BasePO implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "merchant_id")
    private Long merchantId;

    @OneToOne
    @JoinColumn(name = "user_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private UserPO user;


    @Column(name = "password")
    private String password;
    /**
     *
     */
    private BusinessEntity information;

    /**
     * 商户数据
     */
    private MerchantData data;

    /**
     * 关注人数
     */
    private Long follows;

    /**
     * 理由
     */
    @Column(name = "reason")
    private String reason;

    /**
     * 审核状态{@link com.treasure.mall.zbq.common.ienum.live.LiveEnums.AuditStatus}
     */
    @Column(name = "audit_status")
    private LiveEnums.AuditStatus auditStatus;


    /**
     * 保证金
     */
    @Column(name = "earnest_money", scale = 2, precision = 19)
    private BigDecimal earnestMoney = BigDecimal.ZERO;
    /**
     * 余额冗余
     */
    @Column(name = "balance", scale = 2, precision = 19)
    private BigDecimal balance = BigDecimal.ZERO;
    /**
     * 待入余额冗余
     */
    @Column(name = "balance_in_future", scale = 2, precision = 19)
    private BigDecimal balanceInFuture = BigDecimal.ZERO;
    /**
     * 历史总收入
     */
    @Column(name = "history_total_amount")
    private BigDecimal historyTotalAmount;

    /**
     * 版本号
     */
    @Column(name = "zb_version")
    private long version;


    /**
     * 启用商户客服功能
     */
    @Column(name = "enable_contact")
    private boolean enableContact;


    /**
     * 佣金比例
     */
    @Column(name = "commission_rate", scale = 3, precision = 6)
    private BigDecimal commissionRate;

    /**
     * 一级返利比例
     */
    @Column(name = "rebate_rate_one", scale = 3, precision = 6)
    private BigDecimal rebateRateOne;

    /**
     * 二级返利比例
     */
    @Column(name = "rebate_rate_two", scale = 3, precision = 6)
    private BigDecimal rebateRateTwo;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singletonList(new SimpleGrantedAuthority(RoleEnum.MERCHANT.getCode()));
    }

    @Override
    public String getUsername() {
        return user.getMobile();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public RoleEnum getRoleType() {
        return RoleEnum.MERCHANT;
    }

    @Override
    public Long getId() {
        return merchantId;
    }

    @Override
    public String getDisplayName() {
        // TODO: 2020/6/12
        return null;
    }

    public MerchantPO() {
        this.balance = BigDecimal.ZERO;
        this.balanceInFuture = BigDecimal.ZERO;
        this.historyTotalAmount = BigDecimal.ZERO;
        this.enableContact = false;
    }
}
